package com.project.zsxd.server.common.util;

import org.springframework.beans.BeanUtils;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * 拷贝工具
 * Created by kaler on 2020/5/16.
 */
public class CommonUtil {

    /**
     * 拷贝单个Bean
     * @param source
     * @param target
     * @param <S>
     * @param <T>
     */
    public static <S,T> void copyBean (S source, T target) {
        BeanUtils.copyProperties(source, target);
    }

    /**
     * 对象是否为空
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        if (obj instanceof String) {
            return isEmpty((String) obj);
        } else if (obj instanceof Long) {
            return isEmpty((Long) obj);
        } else if (obj instanceof Date) {
            return isEmpty((Date) obj);
        } else if (obj instanceof Collection) {
            return isEmpty((Collection) obj);
        } else if (obj instanceof Map) {
            return isEmpty((Map) obj);
        } else if (obj != null && obj.getClass().isArray()) {
            return isEmptyArray(obj);
        } else {
            return isNull(obj);
        }
    }

    private static boolean isEmptyArray(Object array) {
        int length;
        if (array instanceof int[]) {
            length = ((int[]) array).length;
        } else if (array instanceof byte[]) {
            length = ((byte[]) array).length;
        } else if (array instanceof short[]) {
            length = ((short[]) array).length;
        } else if (array instanceof char[]) {
            length = ((char[]) array).length;
        } else if (array instanceof float[]) {
            length = ((float[]) array).length;
        } else if (array instanceof double[]) {
            length = ((double[]) array).length;
        } else if (array instanceof long[]) {
            length = ((long[]) array).length;
        } else if (array instanceof boolean[]) {
            length = ((boolean[]) array).length;
        } else {
            length = ((Object[]) array).length;
        }
        if (length == 0) {
            return true;
        }
        return false;
    }


    public static boolean isEmpty(Date aDate) {
        if (aDate == null) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isEmpty(Long aLong) {
        if (aLong == null) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean isEmpty(Map m) {
        if (m == null || m.isEmpty()) {
            return true;
        }
        return false;
    }

    public static boolean isEmpty(Collection c) {
        if (c == null || c.isEmpty()) {
            return true;
        }
        return false;
    }

    public static boolean isEmpty(String aStr) {
        if (aStr == null || aStr.trim().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isNull(Object oStr) {
        if (oStr == null) {
            return true;
        } else {
            return false;
        }
    }
}
